/**
 * Created by George on 2018/3/19.
 */
define(['angular', 'util', 'jquery', 'ueditor-config', 'ueditor'], function (angular, util, jquery, ueditorConfig, ueditor) {
    "use strict";
    // 将本控制器函数作为结果返回给router.js
    var editNoteCtrl = function ($scope, $http, $stateParams) {
        $scope.welcome_info = "笔记管理";
        $scope.note = {
            id: "",
            name: "",
            date: util.getNowFormatDate(),
            content: ""
        };
        UE.delEditor('containerEditor');
        var containerEditor = UE.getEditor('containerEditor', {
            //这里可以选择自己需要的工具按钮名称,此处仅选择如下七个
            toolbars: [['fullscreen', 'source', 'undo', 'redo', 'bold', 'italic', 'underline']],
            //focus时自动清空初始化时的内容
            autoClearinitialContent: true,
            //关闭字数统计
            wordCount: false,
            //关闭elementPath
            elementPathEnabled: false,
            //默认的编辑区域高度
            initialFrameHeight: 200,
            initialFrameWidth: getTextWidth(),
            scaleEnabled: false,
            serverUrl: ""
            //更多其他参数，请参考umeditor.config.js中的配置项
        });

        function getTextWidth() {
            return $("#noteContent").width() * 0.8;
        }

        $scope.editNoteClick = function () {
            $scope.note.content = containerEditor.getContent();
            console.log($scope.note);
            var data = {
                "id": $scope.note.id,
                "name": $scope.note.name,
                "content": $scope.note.content,
                "date": $scope.note.date
            };
            $http.put('/basketballfire/rest/note', data).then(function (req) {
                console.log("success!");
                alert("修改成功！");
                window.location.href = "/basketballfire/index.html#/note";
            }, function (req) {
                console.log("failed!");
                alert("修改失败！");
            });
        };

        $scope.CancelClick = function () {
            console.log("click cancel");
            window.location.href = "/basketballfire/index.html#/note";
        };

        function init() {
            initOrUpdateData();
        }

        function initOrUpdateData() {
            $scope.note = {
                id: "",
                name: "",
                date: util.getNowFormatDate(),
                content: ""
            };

            var noteID = $stateParams.noteId;

            $http.get("/basketballfire/rest/note?id=" + noteID).then(function (data, status) {
                console.log(data);
                var noteData = data.data;
                $scope.note = {
                    id: noteData.id,
                    name: noteData.name,
                    content: noteData.content,
                    date: noteData.date
                };
                containerEditor.addListener("ready", function () {
                    console.log("set content=" + $scope.note.content);
                    containerEditor.setContent($scope.note.content);
                });

            }, function (req) {
                console.log("failed!");
                alert("note not exist！");
                window.location.href = "/basketballfire/index.html#/note";
            });
        }


        //app start
        init();
    };

    return editNoteCtrl;
});